﻿@{
    ViewBag.Title = "Ins";
}
@section scripts
{   
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />       
    <script src="@Url.Content("~/Scripts/shared/share.js?v=")@ViewBag.version" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js?v=")@ViewBag.version" type="text/javascript"></script>  
    <script src="@Url.Content("~/Scripts/Account/InsDataPay.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/Print.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/zxx.drag.1.0.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/export.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/gridlayout.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="~/scripts/common/print.js?v=@ViewBag.version" type="text/javascript"></script>
    <style type="text/css">
        .window-mask {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                filter: alpha(opacity=40);
                opacity: 0.40;
                font-size: 1px;
                overflow: hidden;
                display:none;
                background-color: gray;
            }
    </style>
}
<body>
   <form id="form1" runat="server" method="post" style="height: 100%">
    
    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
        <table width = "1000px;">
            <tr>
                <td width = "1000px;">
                    <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="NewBill" plain="true">新增</a>
                    <span class="separator"></span> 
                    <a class="mini-button" id="setall" name="setall" iconCls="icon-goto" onclick="setAllAmount" plain="true">全付款</a>
                    <a class="mini-button" id="setamt" name="setamt" iconCls="icon-node" onclick="setSomeAmount" plain="true">输入付款</a>
                    <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveBill" plain="true">保存</a>
                    <span class="separator"></span>                
                    <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="modify" plain="true">改单</a>
                    <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="checkBill" plain="true">审核</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="print" iconCls="icon-print" onclick="printBillMyseft" plain="true">打印</a>
                    <a class="mini-button" id="getdata" iconCls="icon-collapse" onclick="getBillData" plain="true">浏览</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindowBill" plain="true">关闭</a>
                </td>
            </tr>
        </table>                 
    </div>
    
    <div id = "DivHeader">
        <table style="width:770px;" cellpadding="0" cellspacing="1">
            <tr>
                <td width ="80px" align="right">供应商：</td>
                <td width ="200px">    
                    <table>
                        <tr>
                            <td width="70px">
                                <input id="FSupplyItemID" name="FSupplyItemID" class="mini-buttonedit" style="width:70px;" 
                                    onbuttonclick="getSupplyItem('')" onvaluechanged ="getSupplyValueChanged" selectOnFocus="true" />
                            </td>
                            <td width="122px">
                                <input id="FSupplyName" name="FSupplyName" class="mini-textbox" style="width:122px;" background-color: #CCCCCC;" 
                                    enabled = "false"  align="left" readonly="readonly"/>
                            </td>
                        </tr>
                    </table>
                </td>
                <td width ="80px" align="right">开单日期：</td>
                <td width ="190px">    
                    <input name="FDate" class="mini-datepicker" style="width:190px;"/>
                </td>
                 <td width ="80px" align="right">单据编号：</td>
                 <td width ="150px">    
                    <input name="FBillNo" class="mini-textbox" required="true"readonly="readonly"style="width:150px;"/>
                 </td>
            </tr>
            <tr>
                <td align="right">付款帐号：</td>
                <td >
                    @Html.Partial("~/Views/Modular/LookupPayItem.cshtml")
                </td>
                <td align="right">单据备注：</td> 
                <td >  
                    <input name="FRemark" class="mini-textbox"  style="width:190px;"/>  
                </td>
                <td align="right">单据状态：</td>
                <td >    
                    <input name="txtCheckFlag" class="mini-textbox" readonly="readonly"style="width:150px;"/></td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">   
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;"
             showPager="false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
             allowCellSelect="true" allowSortColumn="false" allowCellEdit="true" navEditMode="true"
             editNextOnEnterKey="true" editNextRowCell="false"  ondrawcell="onDrawCell" oncellcommitedit="onCellCommitEdit"  
            ondrawsummarycell="onDrawSummaryCell" showSummaryRow="true" fitColumns="false">
            <div property="columns">
                <div type="indexcolumn" width="35" headerAlign="center">序号</div>
                <div field="mFScBillNo" name="mFScBillNo" width="120" headerAlign="center">单据编号</div>
                <div field="mFBillDate" name="mFBillDate" headerCls="CellLast" width="90" dateformat="yyyy-MM-dd" headerAlign="center">单据日期</div>
                <div field="mFSupplyName" name="mFSupplyName" width="220" headerAlign="center">供应商名称</div>
                <div field="mFAmount" name="mFAmount" width="90"  numberFormat="#,0.00#" headerAlign="center" align="right">应付金额</div>
                <div field="mFAmountChk" name="mFAmountChk" width="90" numberFormat="#,0.00#"  headerAlign="center" align="right">已付金额</div>
                <div field="mFAmountUn" name="mFAmountUn" width="90" numberFormat="#,0.00#"  headerAlign="center" align="right">未付金额</div>
                <div field="mFAmountPay" name="mFAmountPay" width="90" numberFormat="#,0.00#"  headerAlign="center" align="right">本次付款
                    <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;" changeOnMousewheel="false" showbutton="false" increment="0" />
                </div>
                <div field="mFAmountDis" name="mFAmountDis" width="90" numberFormat="#,0.00#"  headerAlign="center" align="right">优惠金额
                    <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;" changeOnMousewheel="false" showbutton="false" increment="0" />
                </div>
                <div field="mFRemark" name="mFRemark" width="150" headerAlign="center">摘要
                    <input property="editor" class="mini-textbox" maxLength="200" style="width:100%;" />
                </div>
            </div>
        </div>  
    </div>
     @Html.Partial("~/Views/Modular/Bill/Footer.cshtml")
    </form>
    <input id="datatype" name="datatype" value="@ViewBag.type" type="hidden"/>

    <script type="text/javascript">
        mini.parse();

        var form = new mini.Form("form1");
        var grid = mini.get("datagrid1");      
        var uID = parent.window.getSystemAppID();
        var uCode = parent.window.getSystemAppCode();
        var uName = parent.window.getSystemAppName();
        var LastField = "mFRemark";
        var sumFields = "mFAmount,mFAmountChk,mFAmountPay,mFAmountDis,mFAmountUn";

        var id = getQueryString("billid");
        if (id != null) {
            mini.getbyName("KInterID").setValue(id);
        }        
        var datatype = getQueryString("datatype");      

        $(function () {
            //BillState(0);
            InitGrid('InsDataPay');
            mini.getbyName("FPayItemNumber").setValue("17");
            mini.getbyName("FPayItemNumber").setText("01");
            mini.getbyName("FPayItemName").setValue("进货付款");
        });

        LoadData();

        function LoadData() {
            var minterid = mini.getbyName("KInterID").value;
            $.ajax({
                url: '../Bill/GetBillData',
                data: { mSystemAppID: uID, mBillID: minterid, mDataType: datatype },
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    grid.setData(dataR.datadetail);
                    if (dataR.datamaster.length > 0) {
                        var mdata = dataR.datamaster[0];
                        form.setData(mdata);
                        mini.getbyName("FSupplyItemID").setValue(mdata.FSupplyID);
                        mini.getbyName("FSupplyItemID").setText(mdata.FSupplyNumber);
                        mini.getbyName("FSupplyName").setValue(mdata.FSupplyName);
                        mini.getbyName("FPayItemNumber").setValue(mdata.FItemTypeID);
                        mini.getbyName("FPayItemNumber").setText(mdata.FPayItemNumber);
                        mini.getbyName("FPayItemName").setValue(mdata.FPayItemName);                        
                        if (mdata.FCheckFlag == true) {                            
                            BillState(2);
                        }
                        else {
                            BillState(1);
                        }
                        if ((mdata.KInterID == "0") || (mdata.KInterID == null)) {
                            NewBill();                            
                        }
                    }
                    else {
                        NewBill();
                    }
                }
            });
        }
        
        function checkValid() {
            if ((mini.getbyName("FBillerID").getValue() == "") || (mini.getbyName("FBillerID").getValue() == "0")) {
                var newbillerid = parent.window.getSystemAppID();
                mini.getbyName("FBillerID").setValue(newbillerid);
            }

            var MainData = form.getData();
            if (MainData.FSupplyItemID.toString() == "") {
                mini.showTips({ content: "<b>保存失败</b> <br/>供应商不能空值!", state: "danger", x: "center", y: "top", timeout: 3000 });
                return false;
            }
            if (MainData.FPayItemNumber.toString() == "") {
                mini.showTips({ content: "<b>保存失败</b> <br/>付款帐号不能空值!", state: "danger", x: "center", y: "top", timeout: 3000 });
                return false;
            }
            if (grid.getData().length == 0) {
                mini.showTips({ content: "<b>保存失败</b> <br/>没有要保存的数据!", state: "danger", x: "center", y: "top", timeout: 5000 });
                BillState(4);
                return false;
            }
            return true;
        }

        function saveBill() {
            BillState(1);//先锁定
            if (checkValid() == false) {
                BillState(4);//先锁定
                return;
            }
            var MainData = form.getData();
            var cDate = mini.getbyName("FDate").getFormValue();
            var sBillNo = MainData.FBillNo;
            var mjson = "[{'KInterID':'" + MainData.KInterID + "',";
            mjson = mjson + "'FBillNo':'" + MainData.FBillNo + "',";
            mjson = mjson + "'FSystemAppID':'" + uID + "',";
            mjson = mjson + "'FSupplyID':'" + MainData.FSupplyItemID + "',";
            mjson = mjson + "'FItemTypeID':'" + MainData.FPayItemNumber + "',";
            mjson = mjson + "'FDate':'" + cDate + "',";
            mjson = mjson + "'FDataType':'" + datatype + "',";
            mjson = mjson + "'FRemark':'" + MainData.FRemark + "',";
            
            var rows = grid.getData();
            var rows_json = mini.encode(rows);
            var datajson = mjson + "'goods_list':" + rows_json + "}]";
            $.ajax({
                url: '../Account/SaveInsDataPay',
                data: { mdatajson: datajson },
                type: "post",
                cache: false,
                success: function (txt) {
                    var ret = mini.decode(txt);
                    if (ret.id > 0 && ret.FBillNo != '') {
                        mini.getbyName("KInterID").setValue(ret.id);
                        mini.getbyName("FBillNo").setValue(ret.FBillNo);
                        mini.showTips({ content: "<b>保存成功</b>", state: "success", x: "center", y: "top", timeout: 3000 });
                        BillState(1);
                    }
                    else {
                        mini.alert("异常:" + ret.Status);
                        BillState(4);
                    }
                },
                error: function (xhr) {
                    mini.alert("异常:" + xhr.responseText);
                }
            });
        }

        function confirm(text) {
            mini.confirm(text, "提示",
              function (action) {
                  if (action == "ok") {
                      checkBill();
                  }
              }
          );
        }      

        function getSupplyItem(mSearchKey) {
            mini.open({
                url: "../GetItem/Index",
                title: "供应商选择", width: 730, height: 460,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "new", sTbName: "SupplyInfo", sSearchKey: mSearchKey };
                    iframe.contentWindow.SetData(data);//打开GetItemID.js的SetData
                },
                ondestroy: function (action) {
                    if (action.length > 0) {
                        mini.get("FSupplyItemID").setValue(action[0].FItemID);
                        mini.get("FSupplyItemID").setText(action[0].FNumber);
                        mini.get("FSupplyName").setValue(action[0].FName);
                        LoadBillDetail(action[0].FItemID);
                    }
                    else {
                        mini.get("FSupplyItemID").setValue('');
                        mini.get("FSupplyItemID").setText('');
                        mini.get("FSupplyName").setValue('');
                    }
                }
            });
        }

        function getSupplyValueChanged() {
            var sSearchKey = mini.get("FSupplyItemID").getText();
            var mAppID = parent.window.getSystemAppID();
            if (sSearchKey == "") return;
            var sTableCode = "SupplyInfo";
            var json = "[{";
            json = json + "'FSystemAppID':" + "'" + mAppID + "',";
            json = json + "'FTbName':" + "'" + sTableCode + "',";
            json = json + "'FClassID':" + "'0',";
            json = json + "'FNumber':" + "'" + sSearchKey + "'";
            json = json + "}]";
            $.post("../GetItem/GetItemList", { datajson: json }, function (text) {
                grid.setData(null);
                var sdata = mini.decode(text);
                if (sdata.data.length >= 1) {
                    mini.get("FSupplyItemID").setValue(sdata.data[0].FItemID);
                    mini.get("FSupplyItemID").setText(sdata.data[0].FNumber);
                    mini.get("FSupplyName").setValue(sdata.data[0].FName);
                    LoadBillDetail(sdata.data[0].FItemID);
                }
            });
        }

        function LoadBillDetail(sSupplyID) {
            $.ajax({
                url: '../Account/GetBillSource',
                data: { mSystemAppID: uID, mObjectID: sSupplyID, mDataType: "17" },
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    grid.setData(dataR.datadetail);
                }
            });
        }

        function setAllAmount() {
            var rows = grid.getData();
            for (var i = 0, l = rows.length; i < l; i++) {
                var row = rows[i]
                grid.updateRow(row, { mFAmountPay: row.mFAmountUn });
            }
            mini.get("save").setEnabled(true);
        }

        function setSomeAmount() {
            mini.open({
                url: "../Account/NumberInput",
                title: "请输入付款金额", width: 300, height: 150,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { sName: "付款金额" };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    if (action != "") setRealAmount(action);
                }
            });
        }


        function setRealAmount(sRealAmt) {
            var fRealAmt = sRealAmt.toFixed(2);
            var fLastamt = fRealAmt;
            var fRowAmt = 0;
            var rRec = grid.getData().length;
            for (var i = 0; i < rRec; i++) {
                var row = grid.getRow(i);
                if (row.mFAmountUn < fLastamt) {
                    fRowAmt = row.mFAmountUn;
                    grid.cancelEdit();
                    grid.updateRow(row, { mFAmountPay: fRowAmt });
                    fLastamt = fLastamt - fRowAmt;
                }
                else {
                    grid.cancelEdit();
                    grid.updateRow(row, { mFAmountPay: fLastamt });
                    fLastamt = 0;
                }
            }
            mini.get("save").setEnabled(true);
        }

        function ExtraFunc(obj) {
            obj.mFSetQty = 0;
            return obj;
        }

  </script>

</body>